package com.itruth.module.share;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.itruth.data.entity.ShareEntity;
import com.itruth.main.R;

public class ShareAdapter extends BaseAdapter {

	private List<ShareEntity> shares = null;
	
	private LayoutInflater inflater;
	
	public ShareAdapter(Context context, List<ShareEntity> shares) {
		this.shares = shares;
		inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		
		ViewHolder vh = null;
		
		if (null == convertView) {
			convertView = inflater.inflate(R.layout.share_item, null);
			vh = new ViewHolder();
			vh.userName = (TextView)convertView.findViewById(R.id.item_tv_username);
			vh.content = (TextView)convertView.findViewById(R.id.item_tv_content);
			vh.time = (TextView)convertView.findViewById(R.id.item_tv_time);
			convertView.setTag(vh);
		} else {
			vh = (ViewHolder)convertView.getTag();
		}
		ShareEntity entity = shares.get(position);
		
		vh.userName.setText(entity.getAuthor());
		vh.content.setText(entity.getContent());
		vh.time.setText(entity.getShareTime());
		
		return convertView;
	}
	
	@Override
	public int getCount() {
		return shares.size();
	}
	
	@Override
	public Object getItem(int position) {
		return getItem(position);
	}
	
	@Override
	public long getItemId(int position) {
		return position;
	}
	
	class ViewHolder {
		
		private TextView userName;
		
		private TextView content;
		
		private TextView time;
		
	}
	
	public void setData(List<ShareEntity> shares) {
		this.shares = shares;
		this.notifyDataSetChanged();
	}
	
	
}
